package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppJwtUtil;
import com.heima.user.dto.LoginDto;
import com.heima.user.entity.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.IApUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * APP用户信息表 服务实现类
 * </p>
 *
 * @author syl
 * @since 2022-06-28
 */
@Service
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements IApUserService {

    @Override
    public ResponseResult login(LoginDto dto) {
        //        有两种情况：如果电话号码和密码都为空，就使用设备id
        if (dto == null || StringUtils.isBlank(dto.getEquipmentId())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (StringUtils.isNotBlank(dto.getPhone()) && StringUtils.isNotBlank(dto.getPassword())) {
//            就是根据手机号和密码登录的
//        2、根据名称查询用户  select * from ap_user where phone=?
            LambdaQueryWrapper<ApUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApUser::getPhone, dto.getPhone());
            ApUser one = this.getOne(queryWrapper);
//              如果查不到 返回用户不存在的提示
            if (one == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
            }
//         3、比较密码
//               如果密码不一致 返回密码错误的提示
//        再重新加密时注意再带上盐 one.getSalt()
//        把明文密码+one.getSalt() 通过md5加密后再和表中加密后的密码比较
            String password_page = DigestUtils.md5Hex(dto.getPassword() + one.getSalt());
            if (!StringUtils.equals(password_page, one.getPassword())) {
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }

//        4、返回结果
//        {user:{},token:""}
            Map resultMap = new HashMap();
            one.setPassword(""); //去除敏感数据
            one.setSalt("");   //去除敏感数据
            one.setPhone("");  //去除敏感数据
            resultMap.put("user", one);

//        根据JWT生成token
            Map<String, Object> claimMaps = new HashMap<>();
            claimMaps.put("userId", one.getId());
            claimMaps.put("name", one.getName());
            String token = AppJwtUtil.getToken(claimMaps);

            resultMap.put("token", token);

            return ResponseResult.okResult(resultMap);
        } else {
//            根据设备id生成token  userId设置成0

            //        4、返回结果
//        {user:{},token:""}
            ApUser one = new ApUser();
            one.setId(0);
            Map resultMap = new HashMap();
            resultMap.put("user", one);

//        根据JWT生成token
            Map<String, Object> claimMaps = new HashMap<>();
            claimMaps.put("userId", one.getId());
            claimMaps.put("name", "");
            claimMaps.put("equipmentId", dto.getEquipmentId());
            String token = AppJwtUtil.getToken(claimMaps);
            resultMap.put("token", token);
            return ResponseResult.okResult(resultMap);
        }
    }
}
